package com.example.ureport2.demo.config;

import com.bstek.ureport.UReportPropertyPlaceholderConfigurer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * properties配置
 *
 * @author zero
 * @date 2019-04-24
 **/
@Slf4j
@Configuration
@ConditionalOnBean(UReportConfig.class)
public class PropertiesConfig extends UReportPropertyPlaceholderConfigurer implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        // 将配置追加到UReport的配置类中
        setProperties(loadProperties());
    }

    /**
     * 加载配置文件
     */
    private Properties loadProperties() throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("config.properties");
        InputStream inputStream = classPathResource.getInputStream();
        Properties properties = new Properties();
        properties.load(inputStream);

        return properties;
    }
}
